<template>
  <div>
    <UploadExcel :on-success="handelSuccess"></UploadExcel>
  </div>
</template>

<script>
import UploadExcel from '@/components/UploadExcel'
import { importEmployee } from '@/api/employees'
export default {
  name: '',
  components: {
    UploadExcel
  },
  data() {
    return {
      tableData: undefined,
      tableHeader: undefined
    }
  },
  computed: {

  },
  methods: {
    // 解析成功时候会触发的回调函数，它会返回表格的表头和内容。
    async handelSuccess({ results }) {
      // 旧对象的字段对应新对象的字段数组
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const data = results.map(t => {
        const item = {}
        for (const k in t) {
          // userRelations[k] 新key
          // t[k] 旧key对应的value
          // item[userRelations[k]] 新key对应的value
          item[userRelations[k]] = t[k]
        }
        return item
      })
      await importEmployee(data)
      this.$message.success('操作成功')
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
